import Author from '../../components/Author'
import Authorteacher from "../../components/Author_teacher";
import { getPrizeDetail, FirstCheckPrize } from '@/api/prize'
import { getCategoryList, getLevelList, getRankList } from "@/api/rule";
import { ScientificPrizeType, SubjectType, DefaultSubjectType, CompleteUnit, SumUnitCnt,
  UnitRank, FirstLevelDiscipline, ProjectScource, ResultsForm } from '@/utils/variable'
import ElImageViewer from 'element-ui/packages/image/src/image-viewer'
export default {
  name: 'PrizeDetail',
  components: { Author, ElImageViewer, Authorteacher },
  filters: {
    statusFilter(status) {
      const statusMap = {
        '未审核': 'info',
        '通过': 'success',
        '不通过': 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      PDF_vis: false,
      pageNums: 0,
      pdfurl: null,
      form: {},
      activeName: ['check_history'],
      checkHistory: [],
      author_student:[],
      author_teacher:[],
      disabled: true,
      fileData: [], // 保存上传的文件
      sendFormData: new FormData(),
      baseDataFormValidated: false,
      dialogImageUrl: '',
      dialogVisible: false,
      image: '',
      showViewer: false,
      nowImages: [],
      fileList: [],
      fileListBak: [],
      deleteFileId: [],
      departmentList: [],
      // 表单数据
      mediaFormData: {},
      mediaFormDesc: {},
      remarkFormData: {},
      remarkFormDesc: {
        remarks: {
          type: 'textarea',
          label: '备注'
        }
      },
      formData: {},
      formDesc: {
        title: {
          type: "input",
          label: "奖励名称",
          required: true,
          attrs: {
            size: "small"
          },
          style: {
            width: "75%"
          }
        },
        results_name: {
          type: "input",
          label: "成果名称",
          required: true,
          attrs: {
            size: "small"
          },
          style: {
            width: "75%"
          }
        },
        scientific_prize_type: {
          type: "select",
          label: "奖励(科研奖励用)类别",
          required: true,
          options: ScientificPrizeType,
          attrs: {
            filterable: true,
            size: "small"
          },
          style: {
            width: "75%"
          }
        },
        people_cnt: {
          type: "number",
          label: "获奖人数",
          required: true,
          attrs: {
            size: "small",
            controls: false
          },
          style: {
            width: "50%"
          },
          layout: 12
        },
        licence_issuing_authority: {
          type: "input",
          label: "发证机关",
          required: true,
          attrs: {
            size: "small"
          },
          style: {
            width: "75%"
          },
          layout: 12
        },
        prize_date: {
          type: "date",
          required: true,
          label: "获奖日期",
          attrs: {
            size: "small",
            "value-format": "yyyy-MM-dd"
          },
          style: {
            width: "50%"
          },
          layout: 12
        },
        complete_unit: {
          type: "input",
          label: "完成单位",
          required: true,
          default: CompleteUnit,
          attrs: {
            size: "small"
          },
          style: {
            width: "75%"
          },
          layout: 12
        },
        prize_category: {
          type: "select",
          label: "获奖类别",
          required: true,
          options: getCategoryList().then(res => {
            return res;
          }),
          attrs: {
            size: "small"
          },
          style: {
            width: "75%"
          },
          prop: {
            text: "name",
            value: "id"
          },
          layout: 12
        },
        prize_rank: {
          type: "select",
          label: "获奖等级",
          required: true,
          options: getRankList().then(res => {
            return res;
          }),
          attrs: {
            size: "small"
          },
          style: {
            width: "75%"
          },
          prop: {
            text: "name",
            value: "id"
          },
          layout: 12
        },
        prize_level: {
          type: "select",
          label: "获奖级别",
          required: true,
          options: getLevelList().then(res => {
            return res;
          }),
          attrs: {
            size: "small"
          },
          style: {
            width: "75%"
          },
          prop: {
            text: "name",
            value: "id"
          },
          layout: 12
        },
        sum_unit_cnt: {
          type: "select",
          label: "总参加单位数",
          required: false,
          options: SumUnitCnt,
          attrs: {
            size: "small"
          },
          style: {
            width: "75%"
          },
          layout: 12
        },
        unit_rank: {
          type: "select",
          label: "单位排名",
          required: false,
          options: UnitRank,
          attrs: {
            size: "small",
            clearable: true
          },
          style: {
            width: "75%"
          },
          layout: 12
        },
        authorization_num: {
          type: "input",
          label: "奖励批准号",
          required: true,
          attrs: {
            size: "small"
          },
          style: {
            width: "75%"
          },
          layout: 12
        },
        first_level_discipline: {
          type: "select",
          label: "一级学科",
          required: true,
          options: FirstLevelDiscipline,
          attrs: {
            size: "small"
          },
          style: {
            width: "75%"
          },
          layout: 12
        },
        project_source: {
          type: "select",
          label: "项目来源",
          required: true,
          options: ProjectScource,
          attrs: {
            size: "small"
          },
          style: {
            width: "75%"
          },
          layout: 12
        },
        subject_type: {
          type: "radio",
          label: "学科门类",
          required: true,
          options: SubjectType,
          default: DefaultSubjectType,
          layout: 12
        },
        results_form: {
          type: "select",
          label: "成果形式",
          required: true,
          options: ResultsForm,
          attrs: {
            size: "small"
          },
          style: {
            width: "75%"
          },
          layout: 12
        }
      }
    }
  },
  created() {
    const id = this.$route.query.id
    var from = this.$route.query.from
    if (!id) {
      this.$message({
        message: '请点击一个获奖进行查看',
        type: 'info',
        duration: 4000
      })
      this.$store.dispatch('tagsView/delView', this.$route)
      this.$router.push({
        name: from
      })
    } else {
      this.id = id
      this.getData()
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    // 获取数据
    getData() {
      getPrizeDetail({ id: this.$route.query.id }).then(response => {
        this.formData = response
        let author_student=[]
        let author_teacher=[]
        response.authors.map(i => {
          if (i.author_type.indexOf('学生')>-1 ) {
            author_student.push(i);
          } else {
            author_teacher.push(i);
          }
        });
        this.author_student=author_student
        this.author_teacher=author_teacher
        this.formData.reward = [response.category, response.competition, response.institution]
        this.remarkFormData = { remarks: response.remarks }
        this.checkHistory = response.check_history
        var fileList = response.files.map(i => {
          i.url = i.file;
          return i;
        });
        this.fileList = fileList;
        this.fileListBak = JSON.parse(JSON.stringify(fileList));
      })
    },
    init() {
      this.initData()
      this.addBaseFormCss()
    },
    // 初始化数据
    initData() {
      this.sendFormData = new FormData()
      this.baseDataFormValidated = false
    },
    // 为基本信息表单添加css
    addBaseFormCss() {
      const form1 = document.getElementById('form1')
      const tmp = form1.childNodes[0].childNodes[0].childNodes[0].childNodes[0]
      var className = tmp.childNodes[0].getAttribute('class') + ' first-node'
      tmp.childNodes[0].setAttribute('class', className)
    },

    // 图片预览
    handlePictureCardPreview(file) {
      this.fileListView = [file.url];
      this.dialogVisible = true;
    },
    closeViewer() {
      this.showViewer = false
    },
    onSubmit() {
      this.form.id = this.$route.query.id
      FirstCheckPrize(this.form).then(res => {
        this.$store.dispatch('tagsView/delView', this.$route)
        this.$router.push({ name: 'FirstCheckPrizeList' })
        this.$notify.success({
          title: '成功',
          message: res.message,
          showClose: false
        })
      }).catch(res => {
        this.$notify.error({
          title: '失败',
          message: res.response.data.message,
          showClose: false
        })
      })
    },
    // 获取PDF页数
    getNumPages() {
      const loadingTask = pdf.createLoadingTask(this.dialogImageUrl)
      loadingTask.promise.then(pdf => {
        this.pageNums = pdf.numPages
      })
    }
  }
}
